<template>
  <el-dialog title="添加文章" :visible="dialogVisible" @close="closeFn">
    <el-form ref="formData" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <!-- <el-input v-model="form.articleBody"></el-input> -->
        <quillEditor v-model="form.articleBody"></quillEditor>
      </el-form-item>
      <el-form-item label="视频地址">
        <el-input v-model="form.videoURL"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" justify-content="right" class="dialog-footer">
      <el-button @click="closeFn">取消</el-button>
      <el-button type="primary" @click="confirmFn">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, update } from '@/api/hmmm/articles'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

export default {
  components: {
    quillEditor
  },
  props: {
    dialogVisible: {
      type: Boolean
    }
  },
  data () {
    return {
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 关闭弹窗
    closeFn () {
      this.$emit('close-dialog')
      this.form = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$refs.formData.resetFields()
    },
    confirmFn () {
      this.$refs.formData.validate(async value => {
        if (value) {
          if (this.form.id) {
            // 编辑状态
            await update(this.form)
            this.$message.success('修改成功了')
          } else {
            // 添加状态
            await add(this.form)
            this.$message.success('添加成功了')
          }

          this.$emit('update-article')
          this.closeFn()
        }
      })
    }
  }
}
</script>

<style scoped >
</style>
